package com.dbq.fast.business.me;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.dbq.fast.App;
import com.dbq.fast.R;
import com.dbq.fast.business.me.adapter.RewardAdapter;
import com.dbq.fast.business.me.adapter.WithdrawAdapter;
import com.dbq.fast.business.me.api.RewardApi;
import com.dbq.fast.business.me.bean.WithdrawInfo;
import com.dbq.fast.common.base.BaseActivity;
import com.dbq.fast.common.net.RemoteApiCallBack;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class WithdrawActivity extends BaseActivity {

    public static final int PAGE_SIZE = 10;

    @BindView(R.id.has_withdraw_money)
    TextView hasWithdrawMoney;
    @BindView(R.id.tip_no_withdraw)
    TextView noWithdraw;
    @BindView(R.id.withdraw_list)
    RecyclerView withdrawList;
    @BindView(R.id.wx_account)
    TextView wxAccount;
    @BindView(R.id.pbLoad)
    ProgressBar pbLoad;

    WithdrawAdapter adapter;

    private int nextPage = 0;
    private int totalPage = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_withdraw_record);
        ButterKnife.bind(this);

        pbLoad.setVisibility(View.VISIBLE);
        initRecycleView();
        initLoadMoreListener();

        loadData(1);

    }

    private void initLoadMoreListener() {
        withdrawList.setOnScrollListener(new RecyclerView.OnScrollListener() {
            int lastVisibleItem;

            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (nextPage > totalPage) return;

                if (newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItem + 1 == adapter.getItemCount()) {
                    adapter.changeMoreStatus(WithdrawAdapter.LOADING_MORE);

                    loadData(nextPage);

                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
                //最后一个可见的ITEM
                lastVisibleItem = layoutManager.findLastVisibleItemPosition();
            }
        });
    }

    private void loadData(int page) {
        RewardApi.queryWithdrawRecord(page, new RemoteApiCallBack<WithdrawInfo>() {
            @Override
            public void onFail(int status, String message) {
                pbLoad.setVisibility(View.GONE);
                Toast.makeText(App.getContext(), getString(R.string.tip_retry_network_bad), Toast.LENGTH_SHORT).show();
            }

            @SuppressLint("StringFormatInvalid")
            @Override
            public void onSuccess(WithdrawInfo withdrawInfos) {
                if (!isResume) return;

                pbLoad.setVisibility(View.GONE);
                totalPage = withdrawInfos.getTotalPage();
                List<WithdrawInfo.WithdrawListBean> withdrawBeans = withdrawInfos.getWithdrawList();
                if (withdrawBeans == null || withdrawBeans.size() == 0) {
                    noWithdraw.setVisibility(View.VISIBLE);
                    withdrawList.setVisibility(View.GONE);
                    if (adapter != null) {
                        adapter.changeMoreStatus(RewardAdapter.NO_LOAD_MORE);
                    }
                } else {
                    noWithdraw.setVisibility(View.GONE);
                    withdrawList.setVisibility(View.VISIBLE);
                    if (page == 1) {
                        loadData(withdrawBeans);
                    } else {
                        moreData(withdrawBeans);
                    }

                    nextPage = withdrawInfos.getCurPage() + 1;
                }

                String wxAccountStr = withdrawInfos.getWechat();
                if (TextUtils.isEmpty(wxAccountStr)) {
                    wxAccount.setVisibility(View.GONE);
                } else {
                    wxAccount.setVisibility(View.VISIBLE);
                    wxAccount.setText(getString(R.string.wx_account_show, wxAccountStr));
                }
                hasWithdrawMoney.setText("" + withdrawInfos.getWithdraw()/100f);
            }
        });
    }

    private void loadData(List<WithdrawInfo.WithdrawListBean> withdrawInfos) {
        if (adapter != null) {
            adapter.setDate(withdrawInfos);
            adapter.changeMoreStatus(WithdrawAdapter.NO_LOAD_MORE);
        }
    }

    private void moreData(List<WithdrawInfo.WithdrawListBean> withdrawInfos) {
        if (adapter != null) {
            adapter.addFooterData(withdrawInfos);
            if (withdrawInfos.size() == PAGE_SIZE) {
                adapter.changeMoreStatus(RewardAdapter.PULLUP_LOAD_MORE);
            } else {
                adapter.changeMoreStatus(RewardAdapter.NO_LOAD_MORE);
            }
        }
    }

    private void initRecycleView() {
        withdrawList.setLayoutManager(new LinearLayoutManager(this));
        withdrawList.setHasFixedSize(true);

        DividerItemDecoration divider = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
        divider.setDrawable(ContextCompat.getDrawable(this, R.drawable.divider_line));
        withdrawList.addItemDecoration(divider);

        adapter = new WithdrawAdapter();
        withdrawList.setAdapter(adapter);
    }

    @OnClick(R.id.back)
    public void onClick(View view) {
        onBackPressed();
    }
}
